//
// Created by qsy on 2023/8/17.
//

#ifndef PROJECT_CHECK_MODBUS_SLAVE_H
#define PROJECT_CHECK_MODBUS_SLAVE_H
#include "main.h"


#define MODBUS_DEVICE_ID   1

#define READ_HOLDING_REG_03H     3
#define READ_INPUT_REG_04H       4
#define WRITE_SINGLE_COIL_05H    5
#define WRITE_SINGLE_REG_06H     6

#define HOLDREG_LENGTH    300	//	共有107个地址，共有107*2个字节

// ---05H 功能码-------
#define   UNLOCK          18
#define   COMM_CHECK      19
#define   DROP            20


// HMI MODBUS RTU 接收报文封装
typedef struct _MODBUS_MASTER_CMD
{
    uint8_t IDAddr;        // 通讯地址
    uint8_t FunCMD;        // 功能码
    uint8_t RegAddrH;      // 操作寄存器地址--高
    uint8_t RegAddrL;      // 操作寄存器地址--低
    uint8_t DataH   ;      // 数据--高
    uint8_t DataL   ;      // 数据--低
    uint8_t CRCCheckL;     // 校验和 --低
    uint8_t CRCCheckH;     // 校验和 --高

    uint8_t NewFrmFlag;

}MODBUS_MASTER_CMD;

void Modbus_Frm_Check(void) ;
void Modbus_Frm_Respond(void) ;

void Modbus_03HCMD_Handle(void);
void Modbus_04HCMD_Handle(void);
void Modbus_05HCMD_Handle(void);
void Modbus_06HCMD_Handle(void);

uint16_t CRC_Check(uint8_t* data, uint8_t length);
uint8_t uint16_t_GetHighByte(uint16_t data) ;
uint8_t uint16_t_GetLowByte(uint16_t data) ;
uint16_t uint16_t_TranWord(uint8_t dataH, uint8_t dataL) ;


uint16_t U32_GetHighWord(uint32_t data);
uint16_t U32_GetLowWord(uint32_t data);
uint32_t U32_TranWord(uint16_t dataH, uint16_t dataL);


void HoldRegDataUpdate(void) ;


#endif //PROJECT_CHECK_MODBUS_SLAVE_H
